package com.example.common.exception.handler;

import com.example.common.entity.Result;
import com.example.common.enumStatus.ResultCode;
import com.example.common.exception.BadRequestException;
import com.example.common.exception.NoResultException;
import com.example.common.exception.ServerResponseException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author cc
 * @classname GlobalExceptionHandler
 * @description TODO
 * @date 2022/8/11 16:00
 */
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(Exception.class)  //申明捕获哪个异常类
    public Result<String> exception(Exception e) {  //接收异常
        // 注意哦，这里返回类型是自定义响应体
        e.printStackTrace();
        return Result.error();
    }

    @ExceptionHandler(MethodArgumentNotValidException.class)
    public Result<String> methodArgumentNotValidException(MethodArgumentNotValidException e) {
        // 从异常对象中拿到ObjectError对象
        ObjectError objectError = e.getBindingResult().getAllErrors().get(0);
        // 然后提取错误提示信息进行返回
        return Result.error(ResultCode.VALIDATE_FAILED.getCode(), ResultCode.VALIDATE_FAILED.getMsg() + "，" + objectError.getDefaultMessage());
    }

    @ExceptionHandler(MissingServletRequestParameterException.class)
    public Result<String> missingServletRequestParameterException(MissingServletRequestParameterException e) {
        // 然后提取错误提示信息进行返回
        return Result.error(ResultCode.VALIDATE_FAILED.getCode(), ResultCode.VALIDATE_FAILED.getMsg() + "，" + e.getMessage());
    }

    @ExceptionHandler(HttpMessageNotReadableException.class)
    public Result<String> httpMessageNotReadableException(HttpMessageNotReadableException e) {
        // 然后提取错误提示信息进行返回
        e.printStackTrace();
        return Result.error(ResultCode.VALIDATE_FAILED.getCode(), ResultCode.VALIDATE_FAILED.getMsg() + "，" + getMsg(e.getMessage()));
    }

    @ExceptionHandler(NoResultException.class)   //返回无结果
    public Result<String> signatureException(NoResultException e) {
        // 注意哦，这里返回类型是自定义响应体
        return Result.noResult();
    }

    @ExceptionHandler(BadRequestException.class)
    public Result<String> badRequestException(BadRequestException e) {
        // 注意哦，这里返回类型是自定义响应体
        return Result.error(HttpStatus.BAD_REQUEST.value(), e.getMsg());
    }

    @ExceptionHandler(ServerResponseException.class)
    public Result<String> serverResponseException(ServerResponseException e) {
        // 注意哦，这里返回类型是自定义响应体
        return Result.error(e.getMsg());
    }

    private String getMsg(String message) {
        if (StringUtils.isNotBlank(message)) {
            return message.substring(0, message.indexOf(":"));
        }
        return null;
    }
}
